import com.sun.deploy.util.Waiter;
import org.omg.CORBA.PUBLIC_MEMBER;

//线程死锁：在一个同步方法中调用了另一个对象的同步方法，可能产生死锁
public class ThreadDemo3 {
    public static void main(String[] args) {
        new DeadThread();
    }
}

//顾客类的方法
class Customer {
    public synchronized void say(Waitress waitress) {
        System.out.println("顾客：先吃饭再买单");
        waitress.doService();
    }

    public synchronized void doService() {
        System.out.println("顾客：同意了，先买单再吃饭");
    }
}

//服务员类的方法
class Waitress {
    public synchronized void say(Customer customer) {
        System.out.println("服务员：先买单再吃饭");
        customer.doService();
    }

    public synchronized void doService() {
        System.out.println("服务员：同意了，先吃饭再买单");
    }
}

//死锁线程
class DeadThread implements Runnable {
    Customer customer = new Customer();
    Waitress waitress = new Waitress();

    public DeadThread() {
        //这里的this就是一个DeadThread，不传this就没给new DeadThread()上锁。
        new Thread(this).start();
        waitress.say(customer);
    }

    @Override
    public void run() {
        customer.say(waitress);
    }
}
